package org.zjvis.datascience.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjvis.lab.nebula.backend.lib.algorithm.conf.connector.Postgresql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import net.zjvis.lab.nebula.backend.lib.algorithm.conf.Configuration;
import net.zjvis.lab.nebula.backend.lib.algorithm.conf.algorithm.KMeans;
import net.zjvis.lab.nebula.backend.lib.algorithm.conf.connector.CsvFile;
import org.zjvis.datascience.common.dto.TaskDTO;
import org.zjvis.datascience.common.model.ApiResult;
import org.zjvis.datascience.common.util.RestTemplateUtil;
import org.zjvis.datascience.service.TaskService;
import org.zjvis.datascience.service.dag.DAGScheduler;

@RequestMapping("/algorithm/test")
@RestController
@Api(tags = "Algorithm", description = "Algorithm[nebula]")
@Validated
public class AlgoTestController {

    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private final static Logger logger = LoggerFactory.getLogger(AlgoTestController.class);

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private DAGScheduler dagScheduler;

    @Autowired
    private TaskService taskService;

    @Value("${postgres.host}")
    private String default_address;

    @Value("${postgres.port}")
    private int default_port;

    @Value("${postgres.username}")
    private String default_username;

    @Value("${postgres.password}")
    private String default_password;

    @Value("${postgres.database}")
    private String default_db_name;


    @PostMapping(value = "/v2/invoke")
    @ResponseBody
    @ApiOperation(value = "文件类型测试", notes = "文件类型测试")
    public ApiResult<String> invoke2() throws JsonProcessingException {

        Configuration configuration = Configuration.builder()
                .connectorConf(Configuration.ConnectorConf.builder()
                        .input(CsvFile.builder()
                                .filePath("tests/data/iris.csv")
                                .build())
                        .output(CsvFile.builder()
                                .filePath("tests/data/iris-k-means.csv")
                                .build())
                        .build())
                .algorithm(KMeans.builder()
                        .column("sepallength")
                        .column("sepalwidth")
                        .column("petallength")
                        .column("petalwidth")
                        .clusterCount(3)
                        .maxIteration(100)
                        .build())
                .build();

        String string = OBJECT_MAPPER.writeValueAsString(configuration);
        String s = restTemplateUtil.invokeAlgo(string);
        return ApiResult.ok(s);
    }

    @PostMapping(value = "/v3/invoke")
    @ResponseBody
    @ApiOperation(value = "GP连接测试", notes = "GP连接测试")
    public ApiResult<String> invoke3() throws JsonProcessingException {
        Configuration configuration = Configuration.builder()
                .connectorConf(Configuration.ConnectorConf.builder()
                        .input(Postgresql.builder()
                                .host(default_address)
                                .port(default_port)
                                .username(default_username)
                                .password(default_password)
                                .databaseName(default_db_name)
                                .readSql("select * from dataset.g_1646_1661165425726")
                                .build())
                        .output(Postgresql.builder()
                                .host(default_address)
                                .port(default_port)
                                .username(default_username)
                                .password(default_password)
                                .databaseName(default_db_name)
                                .writeTableName("pipeline.view_kmeans_171960_1662359155522")
                                .ifWriteTableExists("replace")
                                .build())
                        .build())
                .algorithm(KMeans.builder()
                        .column("sepallength")
                        .column("sepalwidth")
                        .column("petallength")
                        .column("petalwidth")
                        .clusterCount(3)
                        .maxIteration(100)
                        .build())
                .build();

        String string = OBJECT_MAPPER.writeValueAsString(configuration);
        String s = restTemplateUtil.invokeAlgo(string);
        return ApiResult.ok(s);
    }

    @PostMapping(value = "/v4/invoke")
    @ResponseBody
    @ApiOperation(value = "Task测试", notes = "Task测试 仅限KMEANS")
    public ApiResult<String> invoke4(@RequestParam Long taskId) throws JsonProcessingException {

        //需要先解开AlgPyEnum.java isNeedAlgo方法
        TaskDTO taskDTO = taskService.queryById(taskId);
        Long sessionId = dagScheduler.triggerFullDose(taskDTO.getPipelineId(), taskId, false);
        return ApiResult.ok("");
    }


}